package com.codeying.servlet.car;

import com.codeying.servlet.AbsServlet;
import com.codeying.entity.*;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.*;
import com.codeying.utils.*;
import com.codeying.utils.component.*;

/**
 * 保存和新增汽车
 */
@WebServlet("/car/save")
public class SaveCarServlet extends AbsServlet {

    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        req.setCharacterEncoding("utf-8");
        resp.setHeader("Content-type", "text/html;charset=UTF-8");
        resp.setContentType("application/json; charset=utf-8");
        resp.setCharacterEncoding("UTF-8");
        int res=0;
        Car entityTemp=new Car ();
        //将参数全部转化成Map格式
        Map<String, Object> reqMap = CommonUtils.requestToMap(req);
        String id = (String) reqMap.get("id");
        entityTemp.setId(Integer.parseInt(id));
                                        String showpic = ""; //汽车首图
                                            String showtitle = ""; //汽车标题
                                            String showdesc = ""; //汽车描述
                                            Double price = 0.0;//价格每天
                                            String showdetail = ""; //汽车租赁说明
                                            Date publishtime = null; //发布时间
                                            String carpic = ""; //展示
                                        showpic = (String)reqMap.get("showpic");
            entityTemp.setShowpic (showpic);//String类型参数
                showtitle = (String)reqMap.get("showtitle");
            entityTemp.setShowtitle (showtitle);//String类型参数
                showdesc = (String)reqMap.get("showdesc");
            entityTemp.setShowdesc (showdesc);//String类型参数
                if (reqMap.get("price") != null && !reqMap.get("price").equals("")){
                price = Double.valueOf((String) reqMap.get("price"));
                entityTemp.setPrice (price);
            }
                    showdetail = (String)reqMap.get("showdetail");
            entityTemp.setShowdetail (showdetail);//String类型参数
                carpic = (String)reqMap.get("carpic");
            entityTemp.setCarpic (carpic);//String类型参数

        //新增或更新
        if(entityTemp.getId() == 0 || "".equals(entityTemp.getId())){//新增
        id = String.valueOf(CommonUtils.newId());
        entityTemp.setId (Integer.parseInt(id));
        publishtime = new Date();
        entityTemp.setPublishtime (publishtime);
        //如果有唯一字段，则在此校验 （字段不为空在前端校验）
                                                    if( carService.getBy("showtitle",entityTemp.getShowtitle()) != null){
                resp.getWriter().print(ApiResult.fail("汽车标题 已存在！"));
                return;
                }
                                                                                    //=========添加之前
            
            //=========添加之前
            res = carService.insert(entityTemp);
        }else {
            //=========修改之前
            
            //=========修改之前
            res = carService.updateById(entityTemp);
        }

        if(res !=0){
            resp.getWriter().print(ApiResult.success());//标记保存成功
        }else {
            resp.getWriter().print(ApiResult.fail("保存失败"));//保存失败
        }
    }
}

